package asia.redact.bracket.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;

/**
 * Password Generator - 
 * 
 * Without Internet Access, we have to use words from a word list found locally
 * 
 * This one uses a word list retrieved from http://www.sil.org/linguistics/wordlists/english/wordlist/wordsEn.txt
 * 
 * @author Dave
 *
 */
public class RandomWordsImpl2 implements RandomWords {
	
	private int minLength;
	private List<WordFilter> filters;
	private Random rand = new Random();
	private Set<String> list = new TreeSet<String>();
	public static final String INPUT_LIST = "/wordsEn.txt";
	
	public RandomWordsImpl2() {
		super();
		this.minLength = 6;
	}

	public RandomWordsImpl2(int minLength) {
		super();
		this.minLength = minLength;
	}

	public void addWordFilter(WordFilter filter) {
		if(filters == null) {
			filters = new ArrayList<WordFilter>();
		}
		
		filters.add(filter);

	}

	public int generate() {
		InputStream in = null;
        in = RandomWords.class.getResourceAsStream(INPUT_LIST);
        InputStreamReader reader = new InputStreamReader(in);
        BufferedReader breader = new BufferedReader(reader);
        String item = null;
        try {
			while((item = breader.readLine())!=null){
				list.add(item);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return 0;
	}

	public String getRandomSelection() {
		if(list.size() == 0) throw new RuntimeException("No passwords available in list.");
		int index = rand.nextInt(list.size());
		return (String) list.toArray()[index];
	}

	public Set<String> getList() {
		return list;
	}

}
